const http = require("http")

const server = http.createServer((req,res)=>{
    let a = ""
    req.on("data",(chunk)=>[
        a+=chunk
    ])
    req.on("end",()=>{
        const users = [ { username: 'admin', password: 'password123' } ];
        let url = "http://localhost:9000/login?"+a
        let myurl= new URL(url)
        let username = myurl.searchParams.get("username")
        let password = myurl.searchParams.get("password")
        console.log(username,password)
        users.forEach(e=>{
            console.log(username===e.username && password === e.password)
            if(username===e.username && password === e.password){
                let obj = {
                    code:"200",
                    msg:"登录成功",
                    username:e.username
                }
                res.end(JSON.stringify(obj))
            }else{
                let obj = {
                    code:"401",
                    msg:"登录失败"
                }
                res.end(JSON.stringify(obj))
            }
        })



    })

    if(req.url==="/login"){
        let obj = {
            code:"401",
            msg:"登录失败"
        }
    }
})

server.listen(9000,()=>{
    console.log("启动服务")
})